package com.androidvizlab.bachelor.notifications;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;

/**
 * A class that handles and creates status bar notifications
 * @author The Hive
 */
public class NoticeManager{

	//Notification id's
	public static final int PREDEFINED_NOTIFICATION_ID0 = 1945;
	public static final int PREDEFINED_NOTIFICATION_ID1 = 1946; //Predefined unique identifier for notification
	public static final int PREDEFINE_NOTIFICATION_ID2 = 1947; //Predefined unique identifier for notification
	//Notification tags
	public static final String PREDEFINED_NOTIFICATION_TAG0 = "VzL9865QR";
	public static final String PREDEFINED_NOTIFICATION_TAG1 = "VzL9866QR";//Predefined unique notification tag 1
	public static final String PREDEFINED_NOTIFICATION_TAG2 = "VzL9867QR";//Predefined unique notification tag 2
	
	//FLAGS AND NOTIFICATION TYPE
	public static final int NOTIFICATION_ONGOING = 256;
	public static final int NOTIFICATION_AUTOCANCEL = 257;
	public static final int NOTIFICATION_AUTOCANCEL_CLEAR = 258;
	
	//NOTIFICATION MANAGER
	private NotificationManager notificationManager = null;
	
	private Notification notice = null;
	
	//NOTIFICATION IDENTIFYERS
	private int NOTIFICATION_ID = -1;
	private String NOTIFICATION_TAG = "";
	
	//ICON(DRAWABLES)
	private int NOTIFICATION_ICON_ID = -1; //notification icon id(R.drawable.id)
	
	//CREATION TIME
	private long NOTICE_TIME = System.currentTimeMillis(); //sets the time when the notification was created
	
	//INTENT
	private Intent notificationIntent = null;
	private PendingIntent pendingIntent = null; //
	
	//CONTEXT
	private Context context = null;
	
	public NoticeManager()
	{
		
	}
	
	public NoticeManager(Context context, int notificationID, String notificationTag, int iconID)
	{
		NOTIFICATION_ID = notificationID;
		NOTIFICATION_TAG = notificationTag;
		NOTIFICATION_ICON_ID = iconID;
		this.context = context;
	}
	
	/**
	 * Create a status bar notification with pending intent
	 */
	public void createNotification(Class<?> cls, String title, String contentText)
	{
		notificationManager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
		
		notificationIntent = new Intent(context, cls);
		
		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		
		pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		
		notice = new Notification(NOTIFICATION_ICON_ID, contentText, NOTICE_TIME);
		
		notice.setLatestEventInfo(context, title, contentText, pendingIntent);
		
		notice.defaults |= Notification.DEFAULT_SOUND;
		notice.defaults |= Notification.DEFAULT_VIBRATE;
		
		notificationManager.notify(NOTIFICATION_TAG, NOTIFICATION_ID, notice);
	}
	
	/**
	 * Create a status bar notification without pending intent
	 * @param title the title of the notification
	 * @param content the content or message displayed to the user.
	 * @param notificationType static integers used by this class to determine if the notification is ongoing or can be cleared automatically.
	 */
	public void createNotification(String title, String content, int notificationType)
	{
		notificationManager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
		
		pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
		
		notice = new Notification(NOTIFICATION_ICON_ID, content, NOTICE_TIME);
		
		notice.setLatestEventInfo(context, title, content, pendingIntent);
		
		notice.defaults |= Notification.DEFAULT_SOUND;
		notice.defaults |= Notification.DEFAULT_VIBRATE;
		
		switch(notificationType)
		{
			case NOTIFICATION_ONGOING:
				notice.flags |= Notification.FLAG_ONGOING_EVENT;
				notice.flags |= Notification.FLAG_NO_CLEAR;
				
				notificationManager.notify(NOTIFICATION_TAG, NOTIFICATION_ID, notice);
			break;
			
			case NOTIFICATION_AUTOCANCEL:
				notice.flags |= Notification.FLAG_AUTO_CANCEL;
				
				notificationManager.notify(NOTIFICATION_TAG, NOTIFICATION_ID, notice);
			break;
			
			case NOTIFICATION_AUTOCANCEL_CLEAR:
				notice.flags |= Notification.FLAG_AUTO_CANCEL;
				
				notificationManager.notify(NOTIFICATION_TAG, NOTIFICATION_ID, notice);
				
				clear(NOTIFICATION_TAG, NOTIFICATION_ID);
			break;
		}
		
		
	}
		
	/**
	 * Clears all notification
	 */
	public void clearAll()
	{
		notificationManager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
		
		notificationManager.cancelAll();
	}
	
	/**
	 * Clear a specific notification
	 * @param tag tag of the notification to be cleared
	 * @param id the id of the notification to be cleared
	 */
	public void clear(String tag, int id)
	{
		notificationManager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
		
		notificationManager.cancel(tag, id);
	}
	
	//GETTERS AND SETTERS

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}
}
